package com.flyqiu.common.mybaits.plus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flyqiu.common.mybaits.plus.enums.FlyQiuQueryCondition;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public abstract class AbstractCommonQuery<Z extends Object> extends QueryWrapper<Z> implements BaseQuery {

    protected final Map<String, String> parameterMap = new LinkedHashMap<>();





    /**
     * 构建条件
     *
     * @param ruleModel
     */
    public void constructorCondition(QueryRuleModel ruleModel) {

        String columnValue = ruleModel.getColVal();
        String columnName = ruleModel.getColName();
        String[] split;
        switch (ruleModel.getCondition()) {
            case EQ: // 等于
                this.eq(columnName, columnValue);
                break;
            case GT: // 大于
                this.gt(columnName, columnValue);
                break;
            case GE: // 大于等于
                this.ge(columnName, columnValue);
                break;
            case LT: // 小于
                this.lt(columnName, columnValue);
                break;
            case LE: // 小于等于
                this.le(columnName, columnValue);
                break;
            case NE: // 不等于
                this.ne(columnName, columnValue);
                break;
            case BETWEEN: // 之间
                split = columnValue.split(",");
                if (split.length < 2) {
                    throw new RuntimeException("构建条件查询失败 between :参数：" + columnValue);
                }
                this.between(columnName, split[0], split[1]);
                break;
            case NOT_BETWEEN: // 不在之间
                split = columnValue.split(",");
                if (split.length < 2) {
                    throw new RuntimeException("构建条件查询失败 notBetween :参数：" + columnValue);
                }
                this.notBetween(columnName, split[0], split[1]);
                break;
            case LIKE: // 包含
                this.like(columnName, columnValue);
                break;
            case NOT_LIKE: // 不包含
                this.notLike(columnName, columnValue);
                break;
            case LIKE_LEFT: // 左模糊
                this.likeLeft(columnName, columnValue);
                break;
            case LIKE_RIGHT: // 右模糊
                this.likeRight(columnName, columnValue);
                break;
            case ORDER_BY_ASC: // 正序
                this.orderByAsc(columnName);
                break;
            case ORDER_BY_DESC: // 倒序
                this.orderByDesc(columnName);
                break;
            case IN:
            case NOT_IN:
                final List<String> collect = Arrays.stream(columnValue.split(",")).distinct().collect(Collectors.toList());
                if (collect.isEmpty()) {
                    break;
                }
                if (ruleModel.getCondition() == FlyQiuQueryCondition.IN) {
                    this.in(columnName, collect);
                } else {
                    this.notIn(columnName, collect);
                }
                break;
            case IS_NULL:
                this.isNull(columnName);
                break;
            case IS_NOT_NULL:
                this.isNotNull(columnName);
                break;
            case CUSTOM_SQL: // 自定义 SQL
                this.apply(StringUtils.isNoneEmpty(columnValue), columnValue);
                break;
        }

    }

}
